﻿using Goooodbye.Presentation.WinFormsApp.Utils;
using SimpleInjector;
using System;
using System.Windows.Forms;

namespace Goooodbye.Presentation.WinFormsApp
{
    public partial class FormMain : FormBase
    {
        private readonly Container container;

        public FormMain(Container container)
        {
            this.container = container;
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            foreach (var item in this.menuStrip1.Items)
            {
                SubscribeToMenuItemClickEvents((ToolStripMenuItem)item);
            }

            LoadChildForm("FormWelcome");
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //e.Cancel = MessageBoxUtil.Confirm("确实要退出吗？") != DialogResult.Yes;
        }

        private void tabControl1_DoubleClick(object sender, EventArgs e)
        {
            var tabCtrl = sender as TabControl;
            tabCtrl.TabPages.Remove(tabCtrl.SelectedTab);
            if (tabControl1.TabPages.Count == 0)
            {
                LoadChildForm("FormWelcome");
            }
        }

        private void ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            /*
             * 通过菜单项 Tag 属性值，读取对应窗体类名，执行窗体加载操作
             * 默认通过标签页显示新窗体
             * 弹出窗体或执行其它动作，需自定义
             */

            var menuItem = sender as ToolStripMenuItem;
            var tag = Convert.ToString(menuItem.Tag);

            switch (tag)
            {
                case "Exit":
                    Application.Exit();
                    break;
                case "Options":
                    new FormSettings().ShowDialog();
                    break;
                case "About":
                    new FormAbout().ShowDialog();
                    break;
                default:
                    if (this.tabControl1.SelectedTab.Name == "FormWelcome")
                    {
                        this.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);
                    }
                    LoadChildForm(tag);
                    break;
            }
        }

        private void LoadChildForm(string formClassName)
        {
            if (!String.IsNullOrEmpty(formClassName))
            {
                if (this.tabControl1.TabPages.ContainsKey(formClassName))
                {
                    this.tabControl1.SelectTab(formClassName);
                }
                else
                {
                    var typeName = $"{this.GetType().Namespace}.{formClassName}";
                    var type = Type.GetType(typeName);
                    if (type != null && this.container.GetInstance(type) is Form childForm)
                    {
                        childForm.TopLevel = false;
                        childForm.Dock = DockStyle.Fill;
                        childForm.FormBorderStyle = FormBorderStyle.None;
                        childForm.Show();

                        var newPage = new TabPage(childForm.Text) { Name = formClassName };
                        newPage.Controls.Add(childForm);

                        this.tabControl1.TabPages.Add(newPage);
                        this.tabControl1.SelectedTab = newPage;
                    }
                }
            }
        }

        private void SubscribeToMenuItemClickEvents(ToolStripMenuItem menuItem)
        {
            var tag = Convert.ToString(menuItem.Tag);
            if (!String.IsNullOrEmpty(tag))
            {
                menuItem.Click -= ToolStripMenuItem_Click;  // 防止重复订阅
                menuItem.Click += ToolStripMenuItem_Click;
            }
            foreach (var item in menuItem.DropDownItems)
            {
                if (item is ToolStripMenuItem childMenuItem)    // 过滤分隔符
                {
                    SubscribeToMenuItemClickEvents(childMenuItem);
                }
            }
        }
    }
}